package com.bbx.taxi.client.Adapter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bbx.api.sdk.model.comm.returns.line.Cities;
import com.bbx.api.sdk.model.comm.returns.line.To_Cities;
import com.bbx.api.sdk.model.passanger.Return.Coupon.CouponList;
import com.bbx.api.sdk.model.passanger.Return.Coupon.Coupons;
import com.bbx.taxi.official.R;
import com.bbx.taxi.client.DB.LineDB;
import com.bbx.taxi.client.DB.Util.DBcolumns;
import com.bbx.taxi.client.Util.DateFormat;
import com.bbx.taxi.client.widget.MyViewPager;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 优惠劵
 */
public class CouponAdapter extends BaseAdapter {
	private List<Coupons> list;
	private Activity main;
	private boolean isItemClick = false;
	private LineDB db_line;// 线路数据库
	private int coupon_id;

	public CouponAdapter(Activity context, List<Coupons> list) {
		this.list = list;
		main = context;
		db_line = new LineDB(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;

	}

	public void setCoupon_id(int coupon_id) {
		this.coupon_id = coupon_id;
	}

	public boolean getItemClick() {
		return isItemClick;
	}

	public void setItemClick(boolean isItemClick) {
		this.isItemClick = isItemClick;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewholder;
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(main);
			convertView = (View) inflater.inflate(R.layout.listitem_coupon,
					null);
			viewholder = new ViewHolder();
			viewholder.layout_top = (LinearLayout) convertView
					.findViewById(R.id.layout_top);
			viewholder.sum = (TextView) convertView.findViewById(R.id.sum);
			viewholder.fuhao = (TextView) convertView.findViewById(R.id.fuhao);
			viewholder.type = (TextView) convertView.findViewById(R.id.type);
			viewholder.line = (TextView) convertView.findViewById(R.id.line);
			viewholder.expire_time = (TextView) convertView
					.findViewById(R.id.expire_time);
			convertView.setTag(viewholder);
		} else
			viewholder = (ViewHolder) convertView.getTag();
		
		if(coupon_id==list.get(position).getId()){
			viewholder.layout_top.setBackgroundDrawable(main.getResources().getDrawable(R.drawable.yhq_bg_down));
		}
		else{
			viewholder.layout_top.setBackgroundDrawable(main.getResources().getDrawable(R.drawable.selector_yhj));
		}
		// 没有过期
		if (((Long.parseLong(list.get(position).getExpire_time()) * 1000) >= new Date()
				.getTime())
				|| Long.parseLong(list.get(position).getExpire_time()) == 0) {
			viewholder.sum.setTextColor(main.getResources().getColor(
					R.color.btn_nor));
			viewholder.fuhao.setTextColor(main.getResources().getColor(
					R.color.btn_nor));
		} else {// 过期
			viewholder.sum.setTextColor(main.getResources().getColor(
					R.color.dark_notes));
			viewholder.fuhao.setTextColor(main.getResources().getColor(
					R.color.dark_notes));
		}
        if(Long.parseLong(list.get(position)
				.getExpire_time())==0){
        	viewholder.expire_time.setText(main
    				.getString(R.string.line_expire_time)+main
    				.getString(R.string.line_expire_no));
        }else{
        	viewholder.expire_time.setText(main
    				.getString(R.string.line_expire_time)
    				+ DateFormat.onCNTime(Long.parseLong(list.get(position)
    						.getExpire_time())));
        }
	

		viewholder.sum.setText(list.get(position).getSum()+"");
		if (list.get(position).getMetadata() != null) {
			viewholder.type.setText(getType(list.get(position).getMetadata()
					.getUsabletype()));
			viewholder.line.setText(list.get(position).getLine_CN());

		}
		// viewholder.type2.setText(getType2(list.get(position).getMetadata().getUsabletype()));

		return convertView;

	}

	public class ViewHolder {
		TextView sum, type, line, expire_time, fuhao;
		public LinearLayout layout_top;
	}

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				// main.startActivity(new Intent(main,
				// AddAddressActivity.class));
				break;
			}
		};
	};

	/**
	 * 体验卷类型2
	 */
	public String getType(String order_type) {
		int type;
		try {
			type = Integer.parseInt(order_type);
		} catch (Exception e) {
			return "";
		}
		switch (type) {
		case -1:// 适用全部
			return main.getString(R.string.line_type_all);
		case 1:// 适用拼车
			return main.getString(R.string.line_type_pc);
		case 2:// 适用包车
			return main.getString(R.string.line_type_bc);
		case 4:// 适用快件
			return main.getString(R.string.line_type_kj);
		case 8:// 适用室内
			return main.getString(R.string.line_type_sn);
		}
		return main.getString(R.string.line_type_all);
	}
}
